package com.oriental.manage.controller.specially;

import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.pojo.redis.Response;
import com.oriental.manage.pojo.specially.BatchMessage;
import com.oriental.manage.service.specially.BatchMessageService;
import com.oriental.manage.service.specially.Impl.BatchMessageHelper;
import com.oriental.opcif.common.exception.ServiceException;
import com.oriental.reserve.model.ResponseModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.record.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Author: yutao
 * Date: 2017/4/20
 * Time: 16:07
 * Desc：
 */

@Controller
@Slf4j
@RequestMapping("specially/batchMessage")
public class BatchMessageController {

    @Autowired
    private BatchMessageService batchMessageService;

    @Autowired
    private BatchMessageHelper batchMessageHelper;

    @RequestMapping("init")
    public String init(){
        return "specially/searchBatchMessage";
    }


    @RequestMapping("search")
    @ResponseBody
    public ResponseDTO<Pagination<BatchMessage>> search(@RequestBody Pagination<BatchMessage> pagination){
        ResponseDTO<Pagination<BatchMessage>> responseDTO = new ResponseDTO<>();
        try{
            log.info("特约商户上报审核查询请求参数:{}",pagination);
            List<BatchMessage> batchMessages = batchMessageService.searchBatchMessage(pagination.getQueryBean());
            log.info("特约商户上报审核查询响应:{}",batchMessages);
            if(batchMessages !=null && batchMessages.size()>0){
                pagination.setRowCount(batchMessages.get(0).getRowCount());
            }
            responseDTO.setSuccess(true);
            pagination.setList(batchMessages);
            responseDTO.setObject(pagination);
        }catch (Exception e){
            log.error("查询特约商户上报审核失败:{}", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("查询失败");
        }
        return responseDTO;

    }

    @RequestMapping("queryDetail")
    @ResponseBody
    public ResponseDTO<Pagination> queryDetail(@RequestBody BatchMessage batchMessage){
        ResponseDTO<Pagination> responseDTO = new ResponseDTO<>();
        try {
             batchMessageService.queryDetail(batchMessage,responseDTO);
        }catch (Exception e){
            log.error("查询特约商户上报审核详细失败:{}", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("查询失败");
        }
        return responseDTO;
    }

    @RequestMapping("toDetail")
    public String toDetail(String trnxCode){

        return batchMessageHelper.getUrl(trnxCode);
    }
    @RequestMapping("toFailMsg")
    public String toFailMsg(){
        return "specially/auditFailMsg";
    }

    @RequestMapping("toFailMsgDetail")
    public String toFailMsgDetail(){
        return "specially/toFailMsgDetail";
    }


    @RequestMapping("audit")
    @RequiresPermissions("specially_audit")
    @ResponseBody
    public ResponseDTO<String> audit(@RequestBody List<BatchMessage> list){
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try{
            batchMessageService.audit(list,responseDTO);
        }catch (ServiceException e){
            log.error("批次审核异常:{}",e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("审核异常");
        }catch (Exception e){
            log.error("批次审核异常:",e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("审核异常");
        }
        return responseDTO;
    }



}
